/*     */ package com.zimbra.cs.redolog.op;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.IndexDocument;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.MailboxOperation;
/*     */ import com.zimbra.cs.redolog.RedoLogInput;
/*     */ import com.zimbra.cs.redolog.RedoLogOutput;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IndexItem
/*     */   extends RedoableOp
/*     */ {
/*     */   private int mId;
/*     */   private MailItem.Type type;
/*     */   private boolean mDeleteFirst;
/*     */   private boolean mCommitAllowed;
/*     */   private boolean mCommitAbortDone;
/*     */   private boolean mAttachedToParent;
/*     */   private RedoableOp mParentOp;
/*     */   
/*     */   public IndexItem()
/*     */   {
/*  44 */     super(MailboxOperation.IndexItem);
/*  45 */     this.mId = 0;
/*  46 */     this.type = MailItem.Type.UNKNOWN;
/*  47 */     this.mCommitAllowed = false;
/*  48 */     this.mCommitAbortDone = false;
/*     */   }
/*     */   
/*     */   public IndexItem(int mailboxId, int id, MailItem.Type type, boolean deleteFirst) {
/*  52 */     this();
/*  53 */     setMailboxId(mailboxId);
/*  54 */     this.mId = id;
/*  55 */     this.type = type;
/*  56 */     this.mDeleteFirst = deleteFirst;
/*  57 */     this.mCommitAllowed = false;
/*  58 */     this.mCommitAbortDone = false;
/*     */   }
/*     */   
/*     */   public boolean deferCrashRecovery()
/*     */   {
/*  63 */     return true;
/*     */   }
/*     */   
/*     */   protected String getPrintableData()
/*     */   {
/*  68 */     StringBuffer sb = new StringBuffer("id=");
/*  69 */     sb.append(this.mId).append(", type=").append(this.type);
/*  70 */     return sb.toString();
/*     */   }
/*     */   
/*     */   protected void serializeData(RedoLogOutput out) throws IOException
/*     */   {
/*  75 */     out.writeInt(this.mId);
/*  76 */     out.writeByte(this.type.toByte());
/*  77 */     if (getVersion().atLeast(1, 8)) {
/*  78 */       out.writeBoolean(this.mDeleteFirst);
/*     */     }
/*     */   }
/*     */   
/*     */   protected void deserializeData(RedoLogInput in) throws IOException
/*     */   {
/*  84 */     this.mId = in.readInt();
/*  85 */     this.type = MailItem.Type.of(in.readByte());
/*  86 */     if (getVersion().atLeast(1, 8)) {
/*  87 */       this.mDeleteFirst = in.readBoolean();
/*     */     } else {
/*  89 */       this.mDeleteFirst = false;
/*     */     }
/*     */   }
/*     */   
/*     */   public void redo() throws Exception
/*     */   {
/*  95 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(getMailboxId());
/*     */     MailItem item;
/*     */     try {
/*  98 */       item = mbox.getItemById(null, this.mId, this.type);
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     catch (MailServiceException.NoSuchItemException e)
/*     */     {
/*     */ 
/*     */ 
/* 107 */       return;
/*     */     }
/*     */     try
/*     */     {
/* 111 */       List<IndexDocument> docList = item.generateIndexData();
/* 112 */       mbox.index.redoIndexItem(item, this.mId, docList);
/*     */     }
/*     */     catch (Exception e) {
/* 115 */       ZimbraLog.index.info("Caught exception attempting to replay IndexItem for ID " + this.mId + " item will not be indexed", e);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized boolean commitAllowed()
/*     */   {
/* 145 */     return this.mCommitAllowed;
/*     */   }
/*     */   
/*     */   public synchronized void allowCommit() {
/* 149 */     this.mCommitAllowed = true;
/* 150 */     if (this.mAttachedToParent) {
/* 151 */       commit();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public synchronized void commit()
/*     */   {
/* 162 */     if (ZimbraLog.index.isDebugEnabled()) {
/* 163 */       ZimbraLog.index.debug(toString() + " committed");
/*     */     }
/*     */     
/*     */ 
/* 167 */     if (!this.mCommitAbortDone) {
/* 168 */       super.commit();
/* 169 */       this.mCommitAbortDone = true;
/*     */       
/*     */ 
/* 172 */       this.mCommitAllowed = true;
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void abort() {
/* 177 */     if (!this.mCommitAbortDone) {
/* 178 */       super.abort();
/* 179 */       this.mCommitAbortDone = true;
/*     */       
/*     */ 
/* 182 */       this.mCommitAllowed = true;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public synchronized void setParentOp(RedoableOp op)
/*     */   {
/* 190 */     this.mParentOp = op;
/*     */   }
/*     */   
/*     */   public synchronized void attachToParent() {
/* 194 */     if ((!this.mCommitAllowed) && (!this.mAttachedToParent)) {
/* 195 */       this.mAttachedToParent = true;
/* 196 */       if (this.mParentOp != null)
/* 197 */         this.mParentOp.addChainedOp(this);
/*     */     } else {
/* 199 */       if ((ZimbraLog.index.isDebugEnabled()) && 
/* 200 */         (this.mAttachedToParent) && (!this.mCommitAllowed)) {
/* 201 */         ZimbraLog.index.debug("Committing because attachToParent called twice!");
/*     */       }
/*     */       
/* 204 */       commit();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/redolog/op/IndexItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */